package com.uking.helper;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.utils.Constant;
import com.uking.util.Utils;
import com.uking.util.VKConstant;
import com.uking.ukingmodule.ukingjnic;

import java.util.HashMap;

public class VKPortalImple implements VKPortalInterface {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "VKPortalImple":"VKPortalImple";

    public interface initApplicationParameter {
        void initApplicationParameters(HashMap<String, String> hashMap);

        void setRegisterState(Boolean bool);
    }

    public String activateAuth(String authserver,String apktype,String activatecode) {
        return ukingjnic.getInstance().activateAuth(authserver,apktype,activatecode);
    }

    @Override
    public String registerUser(String authserver, String uid, String apksn) {
        return ukingjnic.getInstance().registerUser(authserver,uid,apksn);
    }

    public boolean registerUser(String strSN, String AAAIP, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key) {
        boolean bool = false;
        boolean notShow = false;
        VKLogger.d(this.TAG, "********" + AAAIP + "*******=====" + strSN + "-------" + PackageName + "------" + port);
        long start = System.currentTimeMillis();
        String authString = ukingjnic.getInstance().registerUser(AAAIP, PackageName, strSN);
        VKConstant.strAAARegisterInfo = authString;
        VKLogger.d(this.TAG, "\u6ce8\u518c3a\u7528\u65f6==" + (System.currentTimeMillis() - start));
        VKLogger.d(this.TAG, "\u6ce8\u518c\u7ed3\u679c  = " + authString);
        if (authString != null && !authString.contains("ERROR_CODE") && authString.length() > 10) {
            bool = true;
            notShow = true;
            String encryptAuthString = VKNetBossUtils.encrypt(authString, key);
            clearSpRegister(false, sharedPre);
            sharedPre.edit().putString(VKConstant.AUTHSTRING_REGISTER, encryptAuthString).commit();
            saveAndInitRegister(sharedPre, authString, initParams, key);
        } else if (count == 2) {
            if (authString != null && authString.contains("ERROR_CODE")) {
                notShow = VKErrorMessageUtil.saveErrorMsge(authString, strSN);
            } else if (authString == null || authString.equals("")) {
                notShow = true;
            }
            String save_authString = VKNetBossUtils.decrypt(sharedPre.getString(VKConstant.AUTHSTRING_REGISTER, ""), key);
            VKLogger.d(this.TAG, "\u66f4\u65b0\u5931\u8d25,\u83b7\u53d6\u672c\u5730\u5b58\u50a8result=" + save_authString);
            if (save_authString == null || save_authString.equals("") || !notShow) {
                bool = false;
            } else {
                authString = save_authString;
                bool = true;
            }
            if (bool && notShow) {
                VKErrorMessageUtil.setEmptyErrorMsage();
            }
        }
        if (authString != null && authString.contains("ERROR_CODE")) {
            notShow = VKErrorMessageUtil.saveErrorMsge(authString, strSN);
        } else if (authString == null || authString.equals("")) {
            notShow = true;
        }
        clearSpRegister(notShow, sharedPre);
        if (bool) {
            VKErrorMessageUtil.setEmptyErrorMsage();
//            this.ukingjnic.initDownloadModul(type, port);
        }
        initParams.setRegisterState(Boolean.valueOf(bool));
        return bool;
    }

    private void saveAndInitRegister(SharedPreferences sharedPre, String response, initApplicationParameter initParams, String key) {
        HashMap<String, String> map = new HashMap();
        String[] arrResponse = response.split("&");
        if (arrResponse != null && arrResponse.length > 0) {
            for (String content : arrResponse) {
                String[] arrContent = content.split("=");
                if (arrContent != null && arrContent.length > 1) {
                    if (arrContent[1] == null) {
                        arrContent[1] = "";
                    }
                    String encryptArr = VKNetBossUtils.encrypt(arrContent[1], key);
                    map.put(arrContent[0], encryptArr);
                    sharedPre.edit().putString(arrContent[0], encryptArr).commit();
                }
            }
        }
        initParams.initApplicationParameters(map);
    }

    private void clearSpRegister(boolean hasNoAuthErr, SharedPreferences sharedPre) {
        if (!hasNoAuthErr) {
            VKLogger.d(this.TAG, "clear register info--");
            sharedPre.edit().clear().commit();
        }
    }

    public boolean whether_can_cached(SharedPreferences sharedPre, int type, int port, initApplicationParameter initParams, String key) {
        boolean bool = false;
        boolean notShow = false;
        if (VKConstant.strAAARegisterInfo != null && VKConstant.strAAARegisterInfo.contains("ERROR_CODE")) {
            notShow = VKErrorMessageUtil.checkErrorMsge(VKErrorMessageUtil.getErrorCode());
        } else if (TextUtils.isEmpty(VKConstant.strAAARegisterInfo)) {
            notShow = true;
        }
        if (notShow) {
            String save_authString = VKNetBossUtils.decrypt(sharedPre.getString(VKConstant.AUTHSTRING_REGISTER, ""), key);
            Utils.log("\u8a3b\u518a\u5931\u8d25,\u83b7\u53d6\u672c\u5730\u5b58\u50a8result=" + save_authString);
            if (TextUtils.isEmpty(save_authString)) {
                bool = false;
            } else {
                VKConstant.strAAARegisterInfo = save_authString;
                bool = true;
            }
        }
        if (bool) {
            Utils.log("\u53ef\u4ee5\u7528AAA\u7de9\u5b58");
            VKErrorMessageUtil.setEmptyErrorMsage();
//            this.ukingjnic.initDownloadModul(type, port);
        } else {
            Utils.log("\u4e0d\u80fd\u7528AAA\u7de9\u5b58");
        }
        initParams.setRegisterState(Boolean.valueOf(bool));
        return bool;
    }
}
